//
//  NEBaseTeamSearchMemberCell.swift
//  NETeamUIKit
//
//  Created by Laura on 2024/7/31.
//

import NECommonUIKit
import UIKit

@objcMembers
open class NEBaseTeamSearchMemberCell: UITableViewCell {
    var currentModel: NETeamMemberInfoModel?
    
    public lazy var headerView: NEUserHeaderView = {
        let header = NEUserHeaderView(frame: .zero)
        header.titleLabel.font = NEConstant.defaultTextFont(14)
        header.titleLabel.textColor = UIColor.white
        header.layer.cornerRadius = 6
        header.clipsToBounds = true
        header.translatesAutoresizingMaskIntoConstraints = false
        return header
    }()
    
    public lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.font = NEConstant.defaultTextFont(16.0)
        label.textColor = .normalTeamTitleColor
        label.accessibilityIdentifier = "id.userName"
        return label
    }()
    
    public lazy var roleLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.font = NEConstant.defaultTextFont(13.0)
        label.textColor = .normalTeamSubTitleColor
        label.accessibilityIdentifier = "id.role"
        return label
    }()
    
    public lazy var ownerLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.font = NEConstant.defaultTextFont(13.0)
        label.textColor = .normalTeamTipTitleColor
        label.textAlignment = .right
        label.text = localizable("team_owner")
        label.accessibilityIdentifier = "id.identify"
        return label
    }()
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        setupUI()
    }
    
    public required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    open func setupUI() {
        contentView.addSubview(headerView)
        NSLayoutConstraint.activate([
            headerView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: kScreenInterval),
            headerView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
            headerView.widthAnchor.constraint(equalToConstant: 40),
            headerView.heightAnchor.constraint(equalToConstant: 40),
        ])
        
        contentView.addSubview(nameLabel)
        NSLayoutConstraint.activate([
            nameLabel.leftAnchor.constraint(equalTo: headerView.rightAnchor, constant: 12.0),
            nameLabel.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -kScreenInterval),
            nameLabel.topAnchor.constraint(equalTo: headerView.topAnchor, constant: -1),
            nameLabel.heightAnchor.constraint(equalToConstant: 22)
        ])
        
        contentView.addSubview(roleLabel)
        NSLayoutConstraint.activate([
            roleLabel.leftAnchor.constraint(equalTo: headerView.rightAnchor, constant: 12.0),
            roleLabel.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -kScreenInterval),
            roleLabel.bottomAnchor.constraint(equalTo: headerView.bottomAnchor, constant: 1),
            roleLabel.heightAnchor.constraint(equalToConstant: 18)
        ])
        
        contentView.addSubview(ownerLabel)
        NSLayoutConstraint.activate([
            ownerLabel.leftAnchor.constraint(equalTo: headerView.rightAnchor, constant: 12.0),
            ownerLabel.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -kScreenInterval),
            ownerLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
            ownerLabel.heightAnchor.constraint(equalToConstant: 18)
        ])
        
    }
    
    func configure(_ model: NETeamMemberInfoModel) {
        // 更新用户信息
        if let userId = model.nimUser?.user?.accountId, let user = NEFriendUserCache.shared.getFriendInfo(userId) {
            model.nimUser = user
        }
        currentModel = model
        if let url = model.nimUser?.user?.avatar, !url.isEmpty {
            headerView.sd_setImage(with: URL(string: url), completed: nil)
            headerView.setTitle("")
        } else {
            headerView.image = nil
            headerView.setTitle(model.showNickInTeam() ?? "")
            headerView.backgroundColor = UIColor.colorWithString(string: model.nimUser?.user?.accountId)
        }
        nameLabel.text = model.atNameInTeam()
        roleLabel.text = TeamHelper.userTitleName(model)
    }
}
